package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.Salary;
import cn.wolfcode.crm.query.EmployeeQueryObject;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.query.SalaryQueryObject;
import cn.wolfcode.crm.service.IEmployeeService;
import cn.wolfcode.crm.service.ISalaryService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("salary")
public class SalaryController {

    @Autowired
    private ISalaryService salaryService;
 @Autowired
    private IEmployeeService employeeService;

    @RequestMapping("list")
    @ResponseBody
    public List<Salary> list() {
        return salaryService.selectAll();
    }

    @RequestMapping("view")
    @RequiresPermissions(value = {"salary:view","工资界面"},logical = Logical.OR)
    public String query(QueryObject qo) {
        return "salary";
    }

    @RequestMapping("saveOrUpdate")
    @RequiresPermissions(value = {"salary:saveOrUpdate","工资新增/更新"},logical = Logical.OR)
    @ResponseBody
    public Object saveOrUpdate(Salary salary) {
        JSONResult result = new JSONResult();
        try {
            salaryService.saveOrUpdate(salary);
        } catch (Exception e) {
            result.remark(e.getMessage());
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("query")
    @ResponseBody
    public PageResult salary(SalaryQueryObject qo) {
        return salaryService.query(qo);
    }

    //导出文件
    @RequestMapping("exportXls")
    public void exportXls(HttpServletResponse resp, EmployeeQueryObject qo) throws IOException {
        //设置下载响应头
        resp.setHeader("Content-Disposition", "attachment;filename=salary.xls");
        //1.创建excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        //创建工作薄
        HSSFSheet sheet = wb.createSheet("salary");

        //把高级查询加入工列表中
        List<Salary> salarys = (List<Salary>) salaryService.query(qo).getRows();

        //创建第一行
        Row row = sheet.createRow(0);
        //设置标题
        row.createCell(0).setCellValue("员工编号");
        row.createCell(1).setCellValue("员工姓名");
        row.createCell(2).setCellValue("月份");
        row.createCell(3).setCellValue("基本工资");
        row.createCell(4).setCellValue("工作天数");
        row.createCell(5).setCellValue("迟到天数");
        row.createCell(6).setCellValue("早退天数");
        row.createCell(7).setCellValue("补签天数");
        row.createCell(8).setCellValue("最终工资");
        row.createCell(9).setCellValue("结算时间");
        for (int i = 0; i < salarys.size(); i++) {
            //从第二行开始创建行
            row = sheet.createRow(i + 1);
            Salary salary = salarys.get(i);
            //创建单元格
            row.createCell(0).setCellValue(salary.getEmpId().getId());
            row.createCell(1).setCellValue(salary.getEmpName().getUsername());
            row.createCell(3).setCellValue(salary.getBasicsalary().doubleValue());
            row.createCell(4).setCellValue(salary.getWorkday());
            row.createCell(5).setCellValue(salary.getLateday());
            row.createCell(6).setCellValue(salary.getEarlyoutday());
            row.createCell(7).setCellValue(salary.getSupplementday());
            row.createCell(8).setCellValue(salary.getFinalsalary().doubleValue());

            //设置输出日期的单元格格式为日期格式操作
            CreationHelper createHelper = wb.getCreationHelper();
            CreationHelper createHelper2 = wb.getCreationHelper();
            CellStyle cellStyle = wb.createCellStyle();
            CellStyle cellStyle2 = wb.createCellStyle();
            //设置索引为2的日期格式
            cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("yyyy-MM"));
            Cell cell = row.createCell(2);
            cell.setCellValue(salary.getAccountday());
            //把格式设置进去
            cell.setCellStyle(cellStyle);
            //设置索引为9的日期格式
            cellStyle2.setDataFormat(createHelper2.createDataFormat().getFormat("yyyy-MM-dd HH:mm:ss"));
            Cell cell1 = row.createCell(9);
            cell1.setCellValue(salary.getAccountday());
            //把格式设置进去
            cell1.setCellStyle(cellStyle2);
        }
        //输出到浏览器
        wb.write(resp.getOutputStream());
    }

    //导入文件
    @RequestMapping("importXls")
    @ResponseBody
    public JSONResult importXls(MultipartFile file) throws IOException {
        JSONResult result = new JSONResult();
        if (file != null && file.getSize() > 0){
            Workbook wb = new HSSFWorkbook(file.getInputStream());
            Sheet sheet = wb.getSheet("salary");
            int rowNum = sheet.getLastRowNum();
            for (int i = 1; i <rowNum ; i++) {
                Row row = sheet.getRow(i);
                Salary salary = new Salary();
                Employee employee = new Employee();
                Cell cell1 = row.getCell(1);
                cell1.setCellType(CellType.STRING);
                String emp = cell1.getStringCellValue();
                long empId = Long.parseLong(emp);
                Employee employee1 = employeeService.selectByPrimaryKey(empId);
                if(employee1 == null){
                    employee.setUsername(row.getCell(2).getStringCellValue());
                    employeeService.saveOrUpdate(employee);
                    Employee employee2 = employeeService.selectByUsername(employee.getUsername());
                    employee.setId(employee2.getId());
                    salary.setEmpId(employee);
                    salary.setEmpName(employee);
                    CreationHelper createHelper = wb.getCreationHelper();
                    CellStyle cellStyle = wb.createCellStyle();
                    //设置日期格式
                    cellStyle.setDataFormat(createHelper.createDataFormat().getFormat("yyyy-MM"));
                    Cell cell = row.getCell(3);
                    cell.setCellStyle(cellStyle);
                    Date date = cell.getDateCellValue();
                    salary.setMonth(date);
                    salary.setBasicsalary(new BigDecimal(row.getCell(4).getStringCellValue()));
                    salary.setWorkday(Long.parseLong(row.getCell(5).getStringCellValue()));
                    salary.setLateday(Long.parseLong(row.getCell(6).getStringCellValue()));
                    salary.setEarlyoutday(Long.parseLong(row.getCell(7).getStringCellValue()));
                    salary.setSupplementday(Long.parseLong(row.getCell(8).getStringCellValue()));
                    Cell cell2 = row.getCell(9);
                    cell2.setCellType(CellType.STRING);
                    salary.setFinalsalary(new BigDecimal(row.getCell(9).getStringCellValue()));

                    CreationHelper createHelper2 = wb.getCreationHelper();
                    CellStyle cellStyle2 = wb.createCellStyle();
                    //设置日期格式
                    cellStyle2.setDataFormat(createHelper.createDataFormat().getFormat("yyyy-MM-dd HH:mm:ss"));
                    Cell cell3 = row.getCell(10);
                    cell3.setCellStyle(cellStyle);
                    date = cell.getDateCellValue();
                    salary.setAccountday(date);
                    salaryService.saveOrUpdate(salary);
                    return result;
                }else {
                    return result.remark("上传失败");
                }

            }
        }
        return result.remark("上传失败");
    }
}
